/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * POS_ItemDescription.java
 *
 * Created on Mar 28, 2011, 2:14:40 PM
 */

package POS_Source;

import DTO.Item;
import POS_Controller.POSController;
import Utilities.MoneyFormatter;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 * this class renders item description view
 * @author EDAYPE
 * @see POS_Source
 */
public class POS_ItemDescription extends javax.swing.JPanel {

    private Item item;
    private POSController controller;

    /** Creates new form POS_MainOut
     * @param control
     * @param item
     */
    public POS_ItemDescription(POSController control, Item item) {
        initComponents();
        controller = control;
        this.item = item;
        customInit();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LabelPanel2 = new javax.swing.JPanel();
        PriceLabel1 = new javax.swing.JLabel();
        PriceField1 = new javax.swing.JTextField();
        ModelLabel1 = new javax.swing.JLabel();
        ModelField1 = new javax.swing.JTextField();
        NameLabel1 = new javax.swing.JLabel();
        NameField1 = new javax.swing.JTextField();
        MakeLabel1 = new javax.swing.JLabel();
        MakeField1 = new javax.swing.JTextField();
        DiscountField1 = new javax.swing.JTextField();
        DiscountLabel1 = new javax.swing.JLabel();
        ImagePanel = new javax.swing.JPanel();
        ImageLabel = new javax.swing.JLabel();
        DescriptionLabel = new javax.swing.JLabel();
        DescriptionPane = new javax.swing.JScrollPane();
        DescriptionField = new javax.swing.JTextArea();
        ButtonLayout = new javax.swing.JPanel();
        TitleLabel = new javax.swing.JLabel();
        ResetButton = new javax.swing.JButton();
        QtyCombo = new javax.swing.JComboBox();
        QtyLabel1 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setPreferredSize(new java.awt.Dimension(800, 600));

        LabelPanel2.setBackground(java.awt.Color.white);

        PriceLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        PriceLabel1.setText("Price");

        PriceField1.setEditable(false);
        PriceField1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

        ModelLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        ModelLabel1.setText("Model");

        ModelField1.setEditable(false);
        ModelField1.setFont(new java.awt.Font("Tahoma", 0, 18));

        NameLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        NameLabel1.setText("Name");

        NameField1.setEditable(false);
        NameField1.setFont(new java.awt.Font("Tahoma", 0, 18));

        MakeLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        MakeLabel1.setText("Make");

        MakeField1.setEditable(false);
        MakeField1.setFont(new java.awt.Font("Tahoma", 0, 18));

        DiscountField1.setEditable(false);
        DiscountField1.setFont(new java.awt.Font("Tahoma", 0, 18));

        DiscountLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        DiscountLabel1.setText("Sale Price!");

        javax.swing.GroupLayout LabelPanel2Layout = new javax.swing.GroupLayout(LabelPanel2);
        LabelPanel2.setLayout(LabelPanel2Layout);
        LabelPanel2Layout.setHorizontalGroup(
            LabelPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(LabelPanel2Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(LabelPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(NameLabel1)
                    .addComponent(MakeLabel1)
                    .addComponent(ModelLabel1)
                    .addComponent(PriceLabel1)
                    .addComponent(DiscountLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(LabelPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(LabelPanel2Layout.createSequentialGroup()
                        .addGroup(LabelPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(MakeField1, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                            .addComponent(NameField1, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                            .addComponent(ModelField1, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE))
                        .addGap(32, 32, 32))
                    .addGroup(LabelPanel2Layout.createSequentialGroup()
                        .addGroup(LabelPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(PriceField1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(DiscountField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE))
                        .addContainerGap(32, Short.MAX_VALUE))))
        );
        LabelPanel2Layout.setVerticalGroup(
            LabelPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(LabelPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(LabelPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NameField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(NameLabel1))
                .addGap(18, 18, 18)
                .addGroup(LabelPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(MakeField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(MakeLabel1))
                .addGap(18, 18, 18)
                .addGroup(LabelPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ModelField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ModelLabel1))
                .addGap(18, 18, 18)
                .addGroup(LabelPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PriceField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(PriceLabel1))
                .addGap(18, 18, 18)
                .addGroup(LabelPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DiscountField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DiscountLabel1))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        ImagePanel.setBackground(new java.awt.Color(255, 255, 255));
        ImagePanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        ImagePanel.setPreferredSize(new java.awt.Dimension(139, 139));

        javax.swing.GroupLayout ImagePanelLayout = new javax.swing.GroupLayout(ImagePanel);
        ImagePanel.setLayout(ImagePanelLayout);
        ImagePanelLayout.setHorizontalGroup(
            ImagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ImageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        ImagePanelLayout.setVerticalGroup(
            ImagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ImageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        DescriptionLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        DescriptionLabel.setText("Description");

        DescriptionField.setColumns(20);
        DescriptionField.setLineWrap(true);
        DescriptionField.setRows(5);
        DescriptionPane.setViewportView(DescriptionField);

        ButtonLayout.setBackground(new java.awt.Color(255, 255, 255));

        TitleLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 50));
        TitleLabel.setText("Item Details");

        ResetButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        ResetButton.setText("Add to Cart");
        ResetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ResetButtonActionPerformed(evt);
            }
        });

        QtyCombo.setFont(new java.awt.Font("Tahoma", 0, 24));
        QtyCombo.setMaximumRowCount(5);
        QtyCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));
        QtyCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                QtyComboActionPerformed(evt);
            }
        });

        QtyLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        QtyLabel1.setText("Qty");

        javax.swing.GroupLayout ButtonLayoutLayout = new javax.swing.GroupLayout(ButtonLayout);
        ButtonLayout.setLayout(ButtonLayoutLayout);
        ButtonLayoutLayout.setHorizontalGroup(
            ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ButtonLayoutLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TitleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
                .addComponent(QtyLabel1)
                .addGap(27, 27, 27)
                .addComponent(QtyCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(ResetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        ButtonLayoutLayout.setVerticalGroup(
            ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonLayoutLayout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(ResetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(TitleLabel)
                        .addComponent(QtyCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(QtyLabel1)))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(DescriptionPane, javax.swing.GroupLayout.PREFERRED_SIZE, 750, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ImagePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(LabelPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(DescriptionLabel)
                    .addComponent(ButtonLayout, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(ImagePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(LabelPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(DescriptionLabel)
                .addGap(18, 18, 18)
                .addComponent(DescriptionPane, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addComponent(ButtonLayout, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
/**
 * this method handles reset button press event
 * @param evt
 */
    private void ResetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ResetButtonActionPerformed
        int qty = QtyCombo.getSelectedIndex()+1;

        controller.addItemToCart(item, qty );
        controller.CartScreen();
    }//GEN-LAST:event_ResetButtonActionPerformed

    private void QtyComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_QtyComboActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_QtyComboActionPerformed
/**
 * this method overrides the default view rendering 
 */
    private void customInit(){
        NameField1.setText(item.getName());
        MakeField1.setText(item.getMake());
        ModelField1.setText(item.getModel());
        PriceField1.setText(new MoneyFormatter().format(item.getRetailPrice()));
        DescriptionField.setText(item.getDescription());
        DescriptionField.setLineWrap(true);
        DescriptionField.setWrapStyleWord(true);
        if (item.getDiscountAmount()>0.0){
            DiscountField1.setVisible(true);
            DiscountLabel1.setVisible(true);
            DiscountField1.setText(new MoneyFormatter().format(item.getRetailPrice()-item.getDiscountAmount()));

        }
        else{
            DiscountField1.setVisible(false);
            DiscountLabel1.setVisible(false);
        }

        BufferedImage image = null;
        try {
           String location = System.getProperty("user.dir");
           location += "/src/POS_Source/images/" + item.getImgURL();
           File imageFile = new File(location);

           image = ImageIO.read(imageFile);
        } catch (IOException e) {
        }
        BufferedImage resizedImage = resize(image,200,200);
        ImageIcon icon = new ImageIcon(resizedImage);
        ImageLabel = new JLabel(icon);
        ImageLabel.setBounds(1,1,200,200);
        ImagePanel.removeAll();
        ImagePanel.add(ImageLabel);
        ImagePanel.validate();
        ImagePanel.repaint();
    }
/**
 * this method resizes the image 
 * @param image
 * @param width
 * @param height
 * @return
 */
    private static BufferedImage resize(BufferedImage image, int width, int height) {
        BufferedImage resizedImage = new BufferedImage(width, height,
        BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(image, 0, 0, width, height, null);
        g.dispose();
        return resizedImage;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel ButtonLayout;
    private javax.swing.JTextArea DescriptionField;
    private javax.swing.JLabel DescriptionLabel;
    private javax.swing.JScrollPane DescriptionPane;
    private javax.swing.JTextField DiscountField1;
    private javax.swing.JLabel DiscountLabel1;
    private javax.swing.JLabel ImageLabel;
    private javax.swing.JPanel ImagePanel;
    private javax.swing.JPanel LabelPanel2;
    private javax.swing.JTextField MakeField1;
    private javax.swing.JLabel MakeLabel1;
    private javax.swing.JTextField ModelField1;
    private javax.swing.JLabel ModelLabel1;
    private javax.swing.JTextField NameField1;
    private javax.swing.JLabel NameLabel1;
    private javax.swing.JTextField PriceField1;
    private javax.swing.JLabel PriceLabel1;
    private javax.swing.JComboBox QtyCombo;
    private javax.swing.JLabel QtyLabel1;
    private javax.swing.JButton ResetButton;
    private javax.swing.JLabel TitleLabel;
    // End of variables declaration//GEN-END:variables

}
